package org.javaweb.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/** 这个Servlet用来测试请求/响应时间。所以中间会让这个线程等待。
 * Created by Administrator on 2016/12/20.
 */
@WebServlet(name="ServletDemo2", urlPatterns = {"/ServletDemo2"})
public class ServletDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            // 休眠10秒再响应
            Thread.sleep(1000*5);
            PrintWriter out = resp.getWriter();
            out.print("wait 10 second");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String p1 = req.getParameter("p1");
        String p2 = req.getParameter("p2");
        PrintWriter out = resp.getWriter();
        out.print("{p1:" + p1 + ",p2:" + p2 + "}");
    }
}
